package com.xydtech.ecard.ecard.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.string.StringUtil;
import com.xydtech.ecard.merchant.domain.EcardBranchSummaryChart;
import com.xydtech.ecard.merchant.service.IEcardBranchSummaryChartService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 电子名片-网格营销汇总统计接口类
 *
 * @author yuzhiguo
 * @date 2024-11-07 下午4:29
 */
@Controller
@Slf4j
@Api(tags = "电子名片-网格营销汇总统计接口")
@RequestMapping("/ecardSaasApi/marketingSummary")
public class EcardMarketingSummaryController extends EcardApiBaseController{

    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private IEcardBranchSummaryChartService ecardBranchSummaryChartService;

    /**
     * 电子名片-我的全部建档拜访数量汇总统计接口
     *
     * @author yuzhiguo
     * @date 2024/11/7 下午4:31
     */
    @PassToken
    @PostMapping(value = "/getSummaryStatistics")
    @ApiOperation(value = "电子名片-我的全部建档拜访数量汇总统计接口", notes = "电子名片-我的全部建档拜访数量汇总统计接口")
    @ResponseBody
    @RsaRequest(param = false)
    public ResponseModel getSummaryStatistics() {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardBranchSummaryChart summaryChart = new EcardBranchSummaryChart();
            if (StringUtil.objIsNullOrEmpty(currentUserId)){
                summaryChart.setMerchantNum(0L);//建档总数
                summaryChart.setMerchantMonthNum(0L);//本月建档总数
                summaryChart.setMerchantCompareNums(0L);//建档较上月对比
                summaryChart.setCompareMerchantType("1");//较上月建档类型（1：上升，2：下降）
                summaryChart.setVisitSum(0L);//拜访总数
                summaryChart.setVisitMouthSum(0L);//本月拜访总数
                summaryChart.setVisitCompareNums(0L);//拜访较上月对比
                summaryChart.setCompareVisitType("1");//较上月拜访类型（1：上升，2：下降）
            } else {
                //获取当前用户的信息
                EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
                JSONObject jsonObject = ecardBranchSummaryChartService.getSummaryStatistics(userInfo.getUserId());
                summaryChart.setMerchantNum(jsonObject.getLong("merchantNum"));//建档总数
                summaryChart.setMerchantMonthNum(jsonObject.getLong("merchantMonthNum"));//本月建档总数
                summaryChart.setMerchantCompareNums(jsonObject.getLong("merchantCompareNums"));//建档较上月对比
                summaryChart.setCompareMerchantType(jsonObject.getString("compareMerchantType"));//较上月建档类型（1：上升，2：下降）
                summaryChart.setVisitSum(jsonObject.getLong("visitSum"));//拜访总数
                summaryChart.setVisitMouthSum(jsonObject.getLong("visitMouthSum"));//本月拜访总数
                summaryChart.setVisitCompareNums(jsonObject.getLong("visitCompareNums"));//拜访较上月对比
                summaryChart.setCompareVisitType(jsonObject.getString("compareVisitType"));//较上月拜访类型（1：上升，2：下降）
            }
            return ResponseModel.okToJSON(summaryChart);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败,请联系管理员!");
        }
    }

}
